package com.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bean.Infotype;
import com.bean.User;
import com.service.InfoService;
import com.service.LoginService;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: LoginController
 * @Description: 登陆作用
 * @author 张鑫
 * @date 2019年8月20日
 *
 */

@Controller
@RequestMapping("/login")
public class LoginController {
	@Resource
	private LoginService loginservice;
	@Resource
	private InfoService infoservice;

	/** @Description:用户登录 */
	@RequestMapping("/login_user")
	@ResponseBody
	public ModelAndView login(@RequestParam(value = "username") String username,
			@RequestParam(value = "password") String password) {
		User user = loginservice.login(username, password);
		List<Infotype> list = infoservice.list_infotype();
		ModelAndView mav = null;
		if (user != null) {
			mav = new ModelAndView("view/IndexTemp");
			mav.addObject("list", list);
		}
		return mav;
	}

	/** @Description:返回主页登录 */
	@RequestMapping("/back_to_index")
	@ResponseBody
	public ModelAndView back_to_index() {
		List<Infotype> list = infoservice.list_infotype();
		ModelAndView mav = null;
		mav = new ModelAndView("view/IndexTemp");
		mav.addObject("list", list);
		return mav;
	}

	/** @Description:信息加入 */
	@RequestMapping("/register")
	public ModelAndView register(@RequestParam(value = "username") String username,
			@RequestParam(value = "password") String password) {
		int flag = loginservice.register(username, password);
		ModelAndView mav = null;
		if (flag == 1) {
			mav = new ModelAndView("index");
		}
		return mav;
	}

	/** @Description:管理员登录 */
	@RequestMapping("/admin_login")
	@ResponseBody
	public ModelAndView admin_login(@RequestParam(value = "username") String username,
			@RequestParam(value = "password") String password, HttpServletRequest request) {
		User user = loginservice.login(username, password);
		ModelAndView mav = null;
		if (user != null) {
			mav = new ModelAndView("pages/admin/view/AdminTemp");
		}
		return mav;
	}

	/** @Description:管理员登录 */
	@RequestMapping("/show_user")
	@ResponseBody
	public ModelAndView show_user(@RequestParam(value = "userid") String userid, HttpServletRequest request) {
		List<User> list = new ArrayList<User>();
		ModelAndView mav = new ModelAndView();
		if (userid.equals("") || userid == null) {
			list = loginservice.list_user();
			/* System.out.println("无id查询"+list.size()); */
		} else {
			list = loginservice.list_userby_userid(Integer.parseInt(userid));
			/* System.out.println("有id查询"+list.size()); */
		}
		mav.addObject("list", list);
		request.setAttribute("mainPage", "/pages/admin/view/usershow.jsp");
		mav.setViewName("pages/admin/view/AdminTemp");
		return mav;
	}

	/** @Description:进入修改页面 */
	@RequestMapping("/openedit_user")
	@ResponseBody
	public ModelAndView openedit_user(@RequestParam(value = "id") int id, HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		request.setAttribute("mainPage", "/pages/admin/view/edituser.jsp");
		mav.addObject("id", id);
		mav.setViewName("pages/admin/view/AdminTemp");
		return mav;
	}

	/** @Description:用戶修改修改 */
	@RequestMapping("/edit_user")
	@ResponseBody
	public ModelAndView edit_user(User user, HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		loginservice.edit_user(user);
		List<User> list = new ArrayList<User>();
		list = loginservice.list_user();
		mav.addObject("list", list);
		request.setAttribute("mainPage", "/pages/admin/view/usershow.jsp");
		mav.setViewName("pages/admin/view/AdminTemp");
		return mav;
	}
	
	/** @Description:刪除用戶 */
	@RequestMapping("/delete_user")
	@ResponseBody
	public ModelAndView delete_user(@RequestParam(value = "id") int id, HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		int flag=loginservice.delete_user(id);
		List<User> list = new ArrayList<User>();
		list = loginservice.list_user();
		mav.addObject("list", list);
		request.setAttribute("mainPage", "/pages/admin/view/usershow.jsp");
		mav.setViewName("pages/admin/view/AdminTemp");
		return mav;
	}
}
